package com.example.myapplication.HomePage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.myapplication.R;
import com.example.myapplication.HomePage.HomePageBean;
import com.example.myapplication.HomePage.HomePageAdapter;
import com.example.myapplication.listview.ListViewActivity;

import java.util.ArrayList;
import java.util.List;

public class HomePageActivity extends AppCompatActivity {
    private List<HomePageBean> date = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        DataHolder.initializeData();
        date = DataHolder.getDate();
        // 设置适配器
        ListView listView = findViewById(R.id.lv);
        listView.setAdapter(new HomePageAdapter(date,this));

        // 设置列表项点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取点击的列表项对应的 HomePageBean
                HomePageBean clickedBean = date.get(position);
                // 获取要跳转的活动类的完整路径
                String pageClassName = clickedBean.getPage();
                try {
                    // 使用反射获取 Class 对象
                    Class<?> pageClass = Class.forName(pageClassName);
                    // 创建 Intent，并指定要跳转的目标活动
                    Intent intent = new Intent(HomePageActivity.this, pageClass);
                    // 启动 Intent 以跳转到目标活动
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    Log.e("HomePageActivity", "Class not found: " + pageClassName);
                }
            }
        });
    }
}

